/**
  ******************************************************************************
  * @file    Templates/Src/stm32f4xx_it.c
  * @author  MCD Application Team
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesQueue.h"
#include "DevicesTime.h"
#include "DevicesTimer.h"
#include "DevicesSoftTimer.h"
#include "DevicesUart.h"

#include "taskMessageRead.h"

/** @addtogroup STM32F4xx_HAL_Examples
  * @{
  */

/** @addtogroup Templates
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
// void SVC_Handler(void)
// {
// }

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
// void PendSV_Handler(void)
// {
// }

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
// void SysTick_Handler(void)
// {
//   HAL_IncTick();
// }

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART1_IRQHandler(void)
{
    volatile static uint32_t uiDMANdtrOld = 0, uiMDANdtrNow = 0;

    while(uiDMANdtrOld != (uiMDANdtrNow = USART1_DMA_READ_LENGTH - g_typeDmaUart1Rx.Instance->NDTR))
    {
        if(uiMDANdtrNow < uiDMANdtrOld)
        {
            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUartSocRead, &g_USART1ReadDMABuff[uiDMANdtrOld], USART1_DMA_READ_LENGTH - uiDMANdtrOld);
            uiDMANdtrOld = 0;
        }

        /* 把一帧数据读取到UART队列缓存里 */
        enumQueuePushDatas(&g_TypeQueueUartSocRead, &g_USART1ReadDMABuff[uiDMANdtrOld], uiMDANdtrNow - uiDMANdtrOld);

        uiDMANdtrOld = uiMDANdtrNow;
    }

    /* 发送信号量 */
    vTaskNotifyGiveFromISR(g_TaskMessageReadHand, NULL);

    /* 清除所有中断标志位 */
    __HAL_UART_CLEAR_IDLEFLAG(&g_typeUart1Handle);
    __HAL_UART_CLEAR_FLAG(&g_typeUart1Handle, UART_FLAG_CTS | UART_FLAG_LBD | UART_FLAG_TXE | UART_FLAG_TC | UART_FLAG_RXNE);
}

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART2_IRQHandler(void)
{
    volatile static uint32_t uiDMANdtrOld = 0, uiMDANdtrNow = 0;

    while(uiDMANdtrOld != (uiMDANdtrNow = USART2_DMA_READ_LENGTH - g_typeDmaUart2Rx.Instance->NDTR))
    {
        if(uiMDANdtrNow < uiDMANdtrOld)
        {
            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUartLogRead, &g_USART2ReadDMABuff[uiDMANdtrOld], USART2_DMA_READ_LENGTH - uiDMANdtrOld);
            uiDMANdtrOld = 0;
        }

        /* 把一帧数据读取到UART队列缓存里 */
        enumQueuePushDatas(&g_TypeQueueUartLogRead, &g_USART2ReadDMABuff[uiDMANdtrOld], uiMDANdtrNow - uiDMANdtrOld);

        uiDMANdtrOld = uiMDANdtrNow;
    }

    /* 发送信号量 */
    vTaskNotifyGiveFromISR(g_TaskMessageReadHand, NULL);

    /* 清除所有中断标志位 */
    __HAL_UART_CLEAR_IDLEFLAG(&g_typeUart2Handle);
    __HAL_UART_CLEAR_FLAG(&g_typeUart2Handle, UART_FLAG_CTS | UART_FLAG_LBD | UART_FLAG_TXE | UART_FLAG_TC | UART_FLAG_RXNE);
}

/*!
    \brief      this function handles USART interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/
void USART6_IRQHandler(void)
{
    volatile static uint32_t uiDMANdtrOld = 0, uiMDANdtrNow = 0;

    while(uiDMANdtrOld != (uiMDANdtrNow = USART6_DMA_READ_LENGTH - g_typeDmaUart6Rx.Instance->NDTR))
    {
        if(uiMDANdtrNow < uiDMANdtrOld)
        {
            /* 把一帧数据读取到UART队列缓存里 */
            enumQueuePushDatas(&g_TypeQueueUartUpperRead, &g_USART6ReadDMABuff[uiDMANdtrOld], USART6_DMA_READ_LENGTH - uiDMANdtrOld);
            uiDMANdtrOld = 0;
        }

        /* 把一帧数据读取到UART队列缓存里 */
        enumQueuePushDatas(&g_TypeQueueUartUpperRead, &g_USART6ReadDMABuff[uiDMANdtrOld], uiMDANdtrNow - uiDMANdtrOld);

        uiDMANdtrOld = uiMDANdtrNow;
    }

    /* 发送信号量 */
    vTaskNotifyGiveFromISR(g_TaskMessageReadHand, NULL);

    /* 清除所有中断标志位 */
    __HAL_UART_CLEAR_IDLEFLAG(&g_typeUart6Handle);
    __HAL_UART_CLEAR_FLAG(&g_typeUart6Handle, UART_FLAG_CTS | UART_FLAG_LBD | UART_FLAG_TXE | UART_FLAG_TC | UART_FLAG_RXNE);
}

/**
  * @brief  This function handles TIM interrupt request.
  * @param  None
  * @retval None
  */
void TIM6_IRQHandler(void)
{
    /* TIM Update event */
    if (__HAL_TIM_GET_FLAG(&g_typeTim6Handle, TIM_FLAG_UPDATE) != RESET)
    {
        if (__HAL_TIM_GET_IT_SOURCE(&g_typeTim6Handle, TIM_IT_UPDATE) != RESET)
        {
            __HAL_TIM_CLEAR_IT(&g_typeTim6Handle, TIM_IT_UPDATE);

            g_lRealTimerHigh += 65536;
        }
    }
}


/**
  * @}
  */

/**
  * @}
  */
